package com.tistory.wonsama.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 문자열 관련 유틸리티
 * @author wonsama
 * @since 2011.12.29
 */
public class WString {

	//////////////////////////////////////////////////////////////
	//
	//	static finals
	//
	//
	public static final String K_FILE_PATH = "k_file_path";
	public static final String K_FILE_NAME = "k_file_name";
	public static final String K_FILE_EXTENSION = "k_file_extension";
	public static final String K_FILE_NAME_WITH_EXT = "k_file_name_with_ext";
	public static final String V_ERROR = "v_error";


	public static final int RANGE_NUMBER 		= 0x1;			//48 ~ 57
	public static final int RANGE_ALPHA_SMALL	= 0x10;		//65 ~ 90
	public static final int RANGE_ALPHA_BIG 		= 0x100;		//97 ~ 122

	/**
	 * <pre>
	 * _ 를 기준으로 입력받은 값을 CAMEL CASE 형태로 변형한다
	 * ex) K_MB_WOW -> kMbWow
	 * </pre>
	 * @param source 입력
	 * @return CAMEL CASE STRING
	 * @since 2011.12.29
	 */
	public static String underToCamelCase(String source)
	{
		String[] ns = source.split("_");

		StringBuffer sb = new StringBuffer();
		sb.append( ns[0].toLowerCase() );
		for(int i=1;i<ns.length;i++)
		{
			sb.append( toCapital( ns[i] ) );
		}

		return sb.toString();
	}

	/**
	 * <pre>
	 * 첫단어만 대문자로 나머지는 소문자로 변형한다
	 * ex) WORLD -> World , world -> World
	 * </pre> 
	 * @return Capital Word 
	 * @since 2011.12.29
	 */
	public static String toCapital(String source)
	{
		return toCapital( source , false );
	}

	/**
	 * <pre>
	 * 첫 문자를 대문자로 변환한다 
	 * onlyFirst가 true인 경우는 첫단어만 적용한다
	 * </pre> 
	 * @return Capital Word 
	 * @since 2011.12.29
	 */
	public static String toCapital(String source, boolean onlyFirst)
	{
		StringBuffer sb = new StringBuffer();
		char[] arrChar;

		if(onlyFirst){
			arrChar = source.toCharArray();
		}else{
			arrChar = source.toLowerCase().toCharArray();	
		}

		sb.append( String.valueOf(arrChar[0]).toUpperCase() );

		for(int i=1;i<arrChar.length;i++)
		{
			sb.append( arrChar[i] );
		}

		return sb.toString();
	}

	/**
	 * 값이 없는 경우 대체한 문자열 값을 출력하도록 한다 
	 * @param source 입력 문자열
	 * @param NVLString 대체 문자
	 * @return 처리 결과
	 * @since 2011.12.29
	 */
	public static String NVL(String source, String NVLString)
	{
		if(source == null || source.trim().equals("") || source.trim().equalsIgnoreCase("NULL"))
			return NVLString;
		else
			return source;
	}

	/**
	 * RPAD : RIGHT PADDING : padingChar '0'
	 * @param source SOURCE STRING
	 * @param size SENTENCE SIZE 
	 * @return PADDING SENTENCE
	 * @since 2011.12.29
	 */
	public static String rPad(String source, int size)
	{
		return rPad( source , size , '0' );
	}

	/**
	 * RPAD : RIGHT PADDING
	 * @param source SOURCE STRING
	 * @param size SENTENCE SIZE 
	 * @param padingChar PADDING STRING
	 * @return PADDING SENTENCE
	 * @since 2011.12.29
	 */
	public static String rPad(String source, int size, char padingChar)
	{
		int len = size - source.length();

		StringBuffer sb = new StringBuffer();
		sb.append( source );
		for (int i=0; i < len ; i++){
			sb.append( padingChar );
		}

		return sb.toString();
	}


	/**
	 * LPAD : LEFT PADDING : padingChar '0'
	 * @param source SOURCE STRING
	 * @param size SENTENCE SIZE 
	 * @return PADDING SENTENCE
	 * @since 2011.12.29
	 */
	public static String lPad(String source, int size)
	{
		return lPad( source , size , '0' );
	}

	/**
	 * LPAD : LEFT PADDING
	 * @param source SOURCE STRING
	 * @param size SENTENCE SIZE 
	 * @param padingChar PADDING CHARACTER
	 * @return PADDING SENTENCE
	 * @since 2011.12.29
	 */
	public static String lPad(String source, int size, char padingChar)
	{
		int len = size - source.length();

		StringBuffer sb = new StringBuffer();
		for (int i=0; i < len ; i++){
			sb.append( padingChar );
		}
		sb.append( source );

		return sb.toString();
	}

	/**
	 * JOIN LIST WORDS
	 * @param source SOURCE SENTENCE
	 * @param joinWord JOIN WORD
	 * @return JOINED SENTENCE
	 * @since 2011.12.29
	 */
	public static String join(String[] source, String joinWord)
	{

		StringBuffer sb = new StringBuffer();
		for(String v:source){
			sb.append( v );
			sb.append( joinWord );
		}

		//remove last join word
		int lastIdx = sb.lastIndexOf(joinWord);
		sb.delete(lastIdx ,sb.length());

		return sb.toString();
	}

	/**
	 * JOIN LIST WORDS
	 * @param source SOURCE SENTENCE
	 * @param joinWord JOIN WORD
	 * @return JOINED SENTENCE
	 * @since 2011.12.29
	 */
	public static String join(List<String> source, String joinWord)
	{

		StringBuffer sb = new StringBuffer();
		for(String v:source){
			sb.append( v );
			sb.append( joinWord );
		}

		//remove last join word
		int lastIdx = sb.lastIndexOf(joinWord);
		sb.delete(lastIdx ,sb.length());

		return sb.toString();
	}

	/**
	 * JOIN LIST WORDS
	 * @param source SOURCE SENTENCE
	 * @param joinWord JOIN WORD
	 * @param fixWord PREFIX & SUFFIX
	 * @return JOINED SENTENCE
	 * @since 2011.12.29
	 */
	public static String join(List<String> source, String joinWord, String fixWord)
	{

		StringBuffer sb = new StringBuffer();
		for(String v:source){
			sb.append( fixWord );
			sb.append( v );
			sb.append( fixWord );
			sb.append( joinWord );
		}

		//remove last join word
		int lastIdx = sb.lastIndexOf(joinWord);
		sb.delete(lastIdx ,sb.length());

		return sb.toString();
	}

	/**
	 * JOIN LIST WORDS
	 * @param source SOURCE SENTENCE
	 * @param joinWord JOIN WORD
	 * @param fixWord PREFIX & SUFFIX
	 * @return JOINED SENTENCE
	 * @since 2011.12.29
	 */
	public static String join(String[] source, String joinWord, String fixWord)
	{

		StringBuffer sb = new StringBuffer();
		for(String v:source){
			sb.append( fixWord );
			sb.append( v );
			sb.append( fixWord );
			sb.append( joinWord );
		}

		//remove last join word
		int lastIdx = sb.lastIndexOf(joinWord);
		sb.delete(lastIdx ,sb.length());

		return sb.toString();
	}

	/**
	 * SPLIT TO STRING LIST 
	 * @param source STRING SOURCE
	 * @param regex REGEX
	 * @return SPLIT STRING LIST
	 * @since 2011.12.29
	 */
	public static List<String> splitToList(String source, String regex)
	{
		String[] _list = source.split( regex );
		List<String> list = new ArrayList<String>(_list.length);

		for(String s:_list){
			list.add(s);
		}

		return list;
	}

	/**
	 * SPLIT TO STRING LIST 
	 * @param source STRING SOURCE
	 * @param regex REGEX
	 * @return SPLIT STRING LIST
	 * @since 2011.12.29
	 */
	public static List<String> splitToListWithTrim(String source, String regex)
	{
		String[] _list = source.split( regex );
		List<String> list = new ArrayList<String>(_list.length);

		for(String s:_list){
			list.add(s.trim());
		}

		return list;
	}

	/**
	 * CHECK SOURCE INCLUDE FIND WORDS
	 * @param source SOURCE SENTENCE
	 * @param find FIND WORDS
	 * @return HAS CONTAIN WORD 
	 * @since 2011.12.29
	 */
	public static boolean hasContain(String source, List<String> find)
	{
		for( String v:find ){
			if( source.indexOf( v ) >= 0 )
				return true;
		}
		return false;
	}




	public static char getRandChar( int range )
	{
		ArrayList<Character> ca = new ArrayList<Character>();

		final int NUM_START = 48;
		final int NUM_END = 57;
		final int BIG_START = 65;
		final int BIG_END = 90;
		final int SMALL_START = 97;
		final int SMALL_END = 122;
		
		if( (range & RANGE_NUMBER ) == RANGE_NUMBER ){ //48 ~ 57
			for( int i=NUM_START ; i<NUM_END ; i++){
				ca.add( (char) i );	
			}
		}
		if( (range & RANGE_ALPHA_SMALL ) == RANGE_ALPHA_SMALL ){ //97 ~ 122
			for( int i=SMALL_START ; i<SMALL_END ; i++){
				ca.add( (char) i );	
			}
		}
		if( (range & RANGE_ALPHA_BIG) == RANGE_ALPHA_BIG ){ 	//65 ~ 90
			for( int i=BIG_START ; i<BIG_END ; i++){
				ca.add( (char) i );	
			}
		}

		Character[] ch = new Character[ ca.size() ];
		ca.toArray( ch );

		return ch[ WMath.getRandInt( 0, ch.length - 1) ] ;
	}
	
	/**
	 * 문자열 목록의 값을 화면에 출력한다 
	 * @param values 값
	 */
	public static void printStrArr( String[] values )
	{
		for(int i=0;i<values.length;i++){
			WLog.debug( i + " : " + values[i] , false );
		}
	}
	
	/**
	 * 문자열 목록의 값을 화면에 출력한다 
	 * @param values 값
	 */
	public static void printStrArr( List<String> values )
	{
		for(int i=0;i<values.size();i++){
			WLog.debug( i + " : " + values.get(i) , false );
		}
	}
}
